package com.rxt.config;

import com.rxt.common.JacksonObjectMapper;
import com.rxt.interceptor.BankHandlerInterceptor;
import com.rxt.interceptor.BankUserLoginInterceptor;
import com.rxt.interceptor.LoginInterceptor;
import com.rxt.interceptor.TokenHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器...");
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0,messageConverter);
    }

    /**
     * 跨域请求配置
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 匹配所有路径
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOriginPatterns("*")
                .maxAge(3600);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenHandlerInterceptor(stringRedisTemplate))
                .addPathPatterns("/**").excludePathPatterns(
                        "/fBankUser/**",
                        "/fFinance/list",
                        "/fFinance/status"
                )
                .order(0);
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(
                        "/uUser/me",
                        "/pBuy",
                        "/kKonwledge/publish",
                        "/oShoppingCart/**",
                        "/logout",
                        "/update",
                        "/oAddressBook",
                        "/oOrders",
                        "/fFinance/add",
                        "/fFinance/own"
                ).order(1);
        registry.addInterceptor(new BankHandlerInterceptor(stringRedisTemplate))
                .addPathPatterns("/fBankUser/**").order(2);
        registry.addInterceptor(new BankUserLoginInterceptor())
                .addPathPatterns(
                        "/fBankUser/me",
                        "/fBankUser/resetPwd",
                        "/fBankUser/update",
                        "/fBankUser/logout"
                ).order(3);

    }


    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
